package com.bestcem.xm.deliver.controller.v2;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.annotation.XmGet;
import com.bestcem.xm.component.mybatis.base.ResultPage;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.deliver.controller.v2.param.BatchSwitchParam;
import com.bestcem.xm.deliver.controller.v2.param.SelectParam;
import com.bestcem.xm.deliver.controller.v2.param.SequenceParam;
import com.bestcem.xm.deliver.controller.v2.param.SwitchParam;
import com.bestcem.xm.deliver.controller.v2.vo.DeliverVo;
import com.bestcem.xm.deliver.controller.v2.vo.SurveyDeliverPageVo;
import com.bestcem.xm.deliver.service.DeliverDefaultDeliverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


@Slf4j
@Api(tags = "投放-V2")
@RestController
@RequiredArgsConstructor
@RequestMapping("/deliver/v2/")
public class DeliverV2DefaultController {

    private @NonNull DeliverDefaultDeliverService defaultDeliverService;

    @ApiOperation(value = "获取链接投放-通用链接")
    @GetMapping("/default/deliver/{id:[a-fA-F0-9]{24}}/detail")
    public DeliverVo getDefaultDeliver(@PathVariable String id) {
        return defaultDeliverService.getDefaultDeliver(id, SecurityContextHolder.getUserId(),
                SecurityContextHolder.getOrgId());
    }

    @ApiOperation(value = "链接投放-通用链接")
    @GetMapping("/surveydeliver/select")
    public ResultPage<SurveyDeliverPageVo> getSurveydeliver(@ApiIgnore Page<SurveyDeliverPageVo> page, @XmGet SelectParam param) {
        return defaultDeliverService.getSelectDeliver(page, param);
    }

    @ApiOperation(value = "获取链接投放-专用链接")
    @GetMapping("/default/sequence/")
    public JSONObject getSequence(@XmGet SequenceParam param) {
        return defaultDeliverService.getDefaultSequence(param);
    }

    @ApiOperation(value = "链接投放-组织链接")
    @GetMapping("/survey_region_exist/{id:[a-fA-F0-9]{24}}")
    public JSONObject surveyRegionExist(@PathVariable String id) {
        return defaultDeliverService.surveyRegionExist(id, SecurityContextHolder.getUserId(),
                SecurityContextHolder.getOrgId());
    }

    @ApiOperation(value = "链接投放-组织链接")
    @GetMapping("/default/deliverstore/{id:[a-fA-F0-9]{24}}")
    public JSONObject getDeliverStore(@PathVariable String id) {
        return defaultDeliverService.getDeliverStore(id, SecurityContextHolder.getUserId(),
                SecurityContextHolder.getOrgId());
    }

    @ApiOperation(value = "通用链接链接-单个启停")
    @PutMapping("/default/deliver/{id:[a-fA-F0-9]{24}}/switch")
    public JSONObject defaultOneSwitch(@PathVariable String id, @RequestBody SwitchParam switchParam) {
        switchParam.setUserId(SecurityContextHolder.getUserId());
        switchParam.setOrgId(SecurityContextHolder.getOrgId());
        return defaultDeliverService.defaultOneSwitch(id, switchParam);
    }


    @ApiOperation(value = "组织链接-批量启停")
    @PutMapping("/default/deliver/store/{id:[a-fA-F0-9]{24}}/batch_switch")
    public JSONObject batchSwitch(@PathVariable String id, @RequestBody BatchSwitchParam batchSwitchParam) {
        batchSwitchParam.setUserId(SecurityContextHolder.getUserId());
        batchSwitchParam.setOrgId(SecurityContextHolder.getOrgId());
        return defaultDeliverService.batchSwitch(id, batchSwitchParam);
    }

    @ApiOperation(value = "组织链接-单个启停")
    @PutMapping("/default/deliver/store/{id:[a-fA-F0-9]{24}}/switch")
    public JSONObject oneSwitch(@PathVariable String id, @RequestBody SwitchParam switchParam) {
        switchParam.setUserId(SecurityContextHolder.getUserId());
        switchParam.setOrgId(SecurityContextHolder.getOrgId());
        return defaultDeliverService.oneSwitch(id, switchParam);
    }

    @ApiOperation(value = "自定义链接-查看进度")
    @GetMapping("/tasks/{id}")
    public JSONObject getDeliverTaskProcess(@PathVariable String id) {
        return defaultDeliverService.getDeliverTaskProcess(id, SecurityContextHolder.getUserId(),
                SecurityContextHolder.getOrgId());
    }
}
